//
//  NhacVietViewController.m
//  NhomThuy02
//
//  Created by Lam Quoc on 2/3/15.
//  Copyright (c) 2015 LamQuoc. All rights reserved.
//

#import "NhacVietViewController.h"
#import "NhacVietTableViewCell.h"
#import "SongDetailViewController.h"

@implementation NhacVietViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.nhacVietTableView.dataSource=self;
    self.nhacVietTableView.delegate=self;
    UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self.overlay action:@selector(longPressDetected:)];
    [self.btnMenu addGestureRecognizer:_longPressRecognizer];
    self.songList = [ManagerData defaultManager].vnSongList;
    self.searchString=@"";
}



#pragma mark - UITableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString* myIdentifier = @"nhacVietID";
    NhacVietTableViewCell* cell = nil;
    cell = [self.nhacVietTableView dequeueReusableCellWithIdentifier:myIdentifier];
    if (cell==nil) {
        [self.nhacVietTableView registerClass:[NhacVietTableViewCell class] forCellReuseIdentifier:myIdentifier];
        cell=[self.nhacVietTableView dequeueReusableCellWithIdentifier:myIdentifier];
        
    }
    NSDictionary* nhacVietDic;
    //CGRect mySize = CGRectMake(0, 0, 310, 133);
    if (![self.searchString isEqualToString:@""]) {
        nhacVietDic = [self.searchArray objectAtIndex:indexPath.row];
    }else{
        nhacVietDic = [self.songList objectAtIndex:indexPath.row];
    }
    cell.maBaiHat.text =[nhacVietDic objectForKey:@"song-code"];
    cell.tenBaiHat.text =[[nhacVietDic objectForKey:@"song-name"]capitalizedString];
    cell.noidungBaiHat.text =[nhacVietDic objectForKey:@"lyrics"];
    [cell.maBaiHat setBackgroundColor:[UIColor greenLightColor]];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (![self.searchString isEqualToString:@""]) {
        return self.searchArray.count;
    }
    return self.songList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}

#pragma mark - Chuyen man hinh
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    NSIndexPath* indexPath;
    NSDictionary* nhacVietDic = [[NSDictionary alloc] init];
    if (![self.searchString isEqualToString:@""]) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        nhacVietDic = [self.searchArray objectAtIndex:indexPath.row];
    }else{
        indexPath = [self.nhacVietTableView indexPathForSelectedRow];
        nhacVietDic = [self.songList objectAtIndex:indexPath.row];
    }
    ((SongDetailViewController*)segue.destinationViewController).songData = [[NSDictionary alloc] initWithDictionary:nhacVietDic];
    
    
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    NSPredicate *pre2 = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        NSDictionary *song =evaluatedObject;
        NSString *name =[song objectForKey:@"song-name"];
        if([[name lowercaseString] containsString:[searchString lowercaseString]]) {
            return YES;
        } else {
            return NO;
        }
    }];
    self.searchArray = [self.songList filteredArrayUsingPredicate:pre2];
    [self.searchDisplayController.searchResultsTableView reloadData];
    self.searchString= searchString;
    return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    self.searchString = @"";
}

@end
